home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / graphics / topsort.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  918b  |  67 lines

  1. #include <LEDA/graph.h>
  2. #include <LEDA/graph_edit.h>
  3.  
  4.  
  5. bool TOPSORT(const graph& G, node_array<int>& ord)
  6.   node_array<int> cur_indeg(G);
  7.  
  8.   list<node> zero_indeg;
  9.  
  10.   int count = 0;
  11.  
  12.   node v,w;
  13.  
  14.   forall_nodes(v,G) 
  15.   { cur_indeg[v] = G.indeg(v);
  16.     if (G.indeg(v)==0) 
  17.        zero_indeg.append(v); 
  18.    }
  19.  
  20.   while ( ! zero_indeg.empty() )
  21.   { 
  22.     count++;
  23.  
  24.     v = zero_indeg.pop();
  25.   
  26.     ord[v] = count;
  27.  
  28.     forall_adj_nodes(w,v) 
  29.        if (--cur_indeg[w]==0) zero_indeg.append(w);
  30.    }
  31.   
  32.   return (count==G.number_of_nodes());
  33. }
  34.      
  35.  
  36.  
  37.  
  38.  
  39. main()
  40. {
  41.   GRAPH<point,int>  G;
  42.  
  43.   window W;
  44.  
  45.   for(;;)
  46.   { 
  47.     graph_edit(W,G);
  48.  
  49.     node_array<int> node_num(G);
  50.  
  51.     if (TOPSORT(G,node_num)==false)
  52.     { W.acknowledge("Graph is cyclic, cannot sort");
  53.       continue;
  54.      }
  55.  
  56.     node v;
  57.     forall_nodes(v,G)
  58.        W.draw_int_node(G[v],node_num[v],blue);
  59.  
  60.    if (W.read_mouse() == 3) break;
  61.  
  62.   }
  63.  
  64.   return 0;
  65. }
  66.